package icasue.runninginvoke.config;

import icasue.base.register.ICasuePluginsRegister;
import icasue.runninginvoke.config.properties.ExposePointProperties;
import icasue.runninginvoke.register.RunningInvokeExposePointScanner;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.AnnotationMetadata;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Author: Qiao Hang
 * @CreateDate: 2021/11/30 下午5:04
 * @UpdateDate:
 * @Description:
 */
public class PluginRunningInvokeBeanRegisterGroup {

    public static class RegisterExposePoint extends ICasuePluginsRegister<ExposePointProperties> {
        @Override
        public String environmentPropertiesPrefix() {
            return "expose.points";
        }

        @Override
        public Class<ExposePointProperties> environmentPropertiesType() {
            return ExposePointProperties.class;
        }

        @Override
        protected @NotNull Boolean checkRegister() {
            return super.configProperties.enable;
        }

        @Override
        public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
            if (configProperties != null) {
                if(configProperties.getPackages().size() > 0){
                    this.exposeInvokablePoint(
                            registry,
                            configProperties.getPackages(),
                            configProperties.getExcludeRegex()
                    );
                }
            }
        }

        private void exposeInvokablePoint(BeanDefinitionRegistry registry, List<String> packages, List<String> excludePackageRegex){
            RunningInvokeExposePointScanner runningInvokeExposePointScanner = new RunningInvokeExposePointScanner(registry, false);
            runningInvokeExposePointScanner.setExcludeRegex(excludePackageRegex);
            //register filters
            runningInvokeExposePointScanner.registerFilters();
            //do find expose point.
            runningInvokeExposePointScanner.doScanGetExposePoint(packages);
        }
    }


    public static class RegisterRunningInvokeApi extends ICasuePluginsRegister {
        @Override
        public String environmentPropertiesPrefix() {
            return null;
        }

        @Override
        public Class environmentPropertiesType() {
            return null;
        }

        @Override
        protected @NotNull Boolean checkRegister() {
            return Boolean.TRUE;
        }

        @Override
        public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
            ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry);
            scanner.scan("icasue.runninginvoke.api");
        }
    }

}
